package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.service.StudentService;
import com.itheima.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1 解决请求和响应中文乱码问题
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //2 获取method标记,根据标记做判断,调用对象的方法处理请求
        String method = request.getParameter("method");
        if("selectByPage".equals(method)){
            selectByPage(request,response);
        }else if("addStudent".equals(method)){
            addStudent(request,response);
        }else if("updateStudent".equals(method)){
            updateStudent(request,response);
        }else if("deleteStudent".equals(method)){
            deleteStudent(request,response);
        }
        //----------------------------


    }

    private void deleteStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.获取请求参数
        String number = request.getParameter("number");
        //2.调用service层方法添加学生信息
        StudentService studentService = new StudentServiceImpl();
        int count = studentService.deleteStudent(number);
        //3.对客户端做出响应
        response.getWriter().write(count+"");
    }

    private void updateStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.获取请求参数,封装成Student对象
        Student student = new Student();
        try {
            BeanUtils.populate(student,request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //2.调用service层方法添加学生信息
        StudentService studentService = new StudentServiceImpl();
        int count = studentService.updateStudent(student);
        //3.对客户端做出响应
        response.getWriter().write(count+"");
    }

    private void addStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.获取请求参数,封装成Student对象
        Student student = new Student();
        try {
            BeanUtils.populate(student,request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //2.调用service层方法添加学生信息
        StudentService studentService = new StudentServiceImpl();
        int count = studentService.addStudent(student);
        //3.对客户端做出响应
        response.getWriter().write(count+"");
    }

    private void selectByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.获取请求参数currentPage和pageSize
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        //2.调用service层方法,获取分页结果pageInfo
        StudentService studentService = new StudentServiceImpl();
        PageInfo<Student> pageInfo = studentService.selectByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        //3.将pageInfo转换成json响应给客户端
        objectMapper.writeValue(response.getWriter(),pageInfo);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
